//
//  GuiderVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/4/18.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "GuiderVC.h"
#import "MRGridLayout.h"
#import "MRLineLayout.h"
#import "GuiderCardCell.h"
#import "GuiderListCell.h"
#import "GuiderDetailVC.h"
#import "UIScrollView+PSRefresh.h"
#import "LinePreDetailVC.h"
#import "AllGuiderVC.h"
@interface GuiderVC ()<UICollectionViewDataSource,UICollectionViewDelegate,collectOrCancelGuiderDelegate>
@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UICollectionView * collectionView;

@property (nonatomic, strong) NSMutableArray * dataSource;

@property (nonatomic, copy) UIButton * menuBtn;

@property (nonatomic) NSUInteger Listtype;

@end

@implementation GuiderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupBg];
    self.barBGAlpha = 0;
    //[self setUpNavigation];
    [self createCollection];
    
   
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self loadDataSource];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"8"];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [MobClick endLogPageView:@"8"];
}

- (void)dealloc {
    
    [MobClick event:@"8_1_1"];
}

//- (void)setUpNavigation {
//
//    _menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    _menuBtn.frame = CGRectMake(0,0,30,30);
//    [_menuBtn addTarget:self action:@selector(changeListType) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_menuBtn];
//    [_menuBtn setImage:[UIImage imageNamed:@"切图_0006_更多"] forState:UIControlStateNormal];
//
//
//}

//- (void)setRefresh {
//
//    WeakSelf(self);
//    [_collectionView addRefreshHeaderWithClosure:^{
//        _pageno = 0;
//        [weakSelf loadDataSource];
//    } addRefreshFooterWithClosure:^{
//        [weakSelf loadDataSource];
//    }];
//
//}
//- (void)setListRefresh {
//
//    __weak typeof(self) weakSelf =self;
//    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        _pageno = 0;
//        [weakSelf loadDataSource];
//    }];
//
//    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [weakSelf loadDataSource];
//    }];
//}

//- (void)endRefresh {
//    if (_Listtype == 0) {
//        [self.collectionView endRefreshing];
//    }else {
//        if (_pageno == 0) {
//            [self.collectionView.mj_header endRefreshing];
//
//        }else {
//            [self.collectionView.mj_footer endRefreshing];
//        }
//        }d8f7
//
//}

- (UICollectionViewFlowLayout *)setLaout {
    //创建自定义布局
    MRLineLayout * layout = [[MRLineLayout alloc] init];
    
    //UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 设置UICollectionView中每个Item的size
    CGFloat width =  SCREEN_WIDTH - 100;
    layout.itemSize = CGSizeMake(width, width +175);
    
    return layout;
}


//- (void)changeListType {
//
//    if ([self.collectionView.collectionViewLayout isKindOfClass:[MRLineLayout class]]) {
//        _Listtype = 1;
//        [_menuBtn setImage:[UIImage imageNamed:@"列表样式"] forState:UIControlStateNormal];
//
//        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
//        layout.sectionInset = UIEdgeInsetsMake(0, 0, 10, 0);
//        layout.itemSize = CGSizeMake(SCREEN_WIDTH,113);
//        //重新设置布局
//        [self.collectionView reloadData];
//        // [self setListRefresh];
//        [self.collectionView setCollectionViewLayout:layout animated:NO];
//    }else {
//        _Listtype = 0;
//        [_menuBtn setImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
//        MRLineLayout * layout = [[MRLineLayout alloc] init];
//
//        // 设置UICollectionView中每个Item的size
//        CGFloat width = ISiPhone5 ?  SCREEN_WIDTH - 100: SCREEN_WIDTH - 100;
//        layout.itemSize = CGSizeMake(width, width +178);
//
//
//        //重新设置布局
//        [self.collectionView reloadData];
//        [self.collectionView setCollectionViewLayout:layout animated:NO];
//        //[self setRefresh];
//    }
//
//
//}

- (void)loadDataSource {
    
    [NetAPIManager getAllGuiderBy:self.currentCityID start:@(0) count:@(0) block:^(NSArray *viewsArray, NSError *error) {
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        self.dataSource =  [NSMutableArray arrayWithArray:viewsArray];
        [self.collectionView reloadData];
        
    }];
    
}
- (void)createCollection {
    
    //创建Collection
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:[self setLaout]];
    _collectionView.backgroundColor = [UIColor redColor];
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.delegate = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerNib:[UINib nibWithNibName:@"GuiderCardCell" bundle:nil] forCellWithReuseIdentifier:@"GuiderCardCell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"GuiderListCell" bundle:nil] forCellWithReuseIdentifier:@"GuiderListCell"];
    
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    [self.view addSubview:_collectionView];
    
    
    
    [self rightBarButton:MYLocalizedString(@"GUIDELIST_HOTGUIDER", nil) color:[UIColor whiteColor] selector:@selector(goAllGuiderVC)];
    
}
- (void)setupBg {
    
    self.view.backgroundColor = kThemeColor;
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _imageView.image = self.Bgimage;
    _imageView.backgroundColor = kThemeColor;
    [self.view addSubview:_imageView];
    
    
}


-(void)goAllGuiderVC {
    
    AllGuiderVC * vc = [[AllGuiderVC alloc] init];
    
    vc.Bgimage = self.imageView.image;

    
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma  mark - UICollectonViewDataSource


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GuiderCardModel * model = self.dataSource[indexPath.row];
    
    
    GuiderCardCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuiderCardCell" forIndexPath:indexPath];
    
    cell.delegate = self;
    [cell.dataSource removeAllObjects];
    [cell configDataWithGuiderCardModel:model];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GuiderCardModel *model = self.dataSource[indexPath.row];
    
    GuiderDetailVC * detailVC = [[GuiderDetailVC alloc] init];
    
    detailVC.guiderID = model.guideid;
    detailVC.delegate = self;
    [self.navigationController pushViewController:detailVC animated:YES];
    [MobClick event:@"8_1_2"];
}

- (void)collectOrCancelGuiderWith:(BOOL)isFaver guiderID:(NSString *)ID{
    
    for (GuiderCardModel * model in self.dataSource) {
        
        if ([model.guideid isEqualToString:ID]) {
            
            model.isfavor = isFaver;
            [self.collectionView reloadData];
            break;
        }
    }
}
#pragma mark -GuiderCardCellDelegate
- (void)selectLine:(LineModel *)model {
    
    LinePreDetailVC *detailVC = [IGUtils loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
    
    detailVC.lineid = model.lineid;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
